package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.PrivateFundEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class PrivateFundDetailProcessor implements PageProcessor{

	private static Logger logger = Logger.getLogger(PrivateFundDetailProcessor.class);
	
	private static final String BASE_URL = "http://ba.amac.org.cn/pages/amacWeb/web-view.html";
	
	private static AtomicInteger atomicInteger = new AtomicInteger(0);
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(page.getContent());
		JSONObject jsonObj = jsonArr.getJSONObject(0);
//		JSONObject retObj = (JSONObject)request.getExtParams().get("retObj");
		int count = atomicInteger.incrementAndGet();
		logger.info("正在解析第"+count+"条数据:");
		PrivateFundEntity pf = new PrivateFundEntity();
		pf.setCreateTime(new Timestamp(System.currentTimeMillis()));
		pf.setUrl(BASE_URL+"?id="+jsonObj.getString("MPI_ID"));
		pf.setClgm(jsonObj.getDouble("CLGM"));
		pf.setClscyhs(jsonObj.getInt("CLSCYHS"));
		pf.setCpbm(jsonObj.getString("CPBM"));
		pf.setCpmc(jsonObj.getString("CPMC"));
		pf.setDqr(jsonObj.getString("DQR"));
		pf.setFedjjg(jsonObj.getString("FEDJJG"));
		pf.setGlfs(jsonObj.getString("GLFS"));
		pf.setGljg(jsonObj.getString("GLJG"));
		pf.setMpiId(jsonObj.getString("MPI_ID"));
		pf.setPwId(jsonObj.getString("PW_ID"));
		pf.setPwStates(jsonObj.getString("PW_STATES"));
		pf.setSffj(jsonObj.getString("SFFJ"));
		pf.setSlrq(DateUtils.parseDate(jsonObj.getString("SLRQ")));
		pf.setTgjg(jsonObj.getString("TGJG"));
		pf.setTzfw(jsonObj.getString("TZFW"));
		pf.setTzlx(jsonObj.getString("TZLX"));
		pf.setId(UuIdPublic.timeStrongID(5));
		StringUtils.removeSpace(pf);
		objectContainer.add(pf);
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
